$().ready(function() {

var db_sb = [0, 2, 5, 10, 16, 26, 42, 64, 99, 150, 227, 342, 515, 774, 1164, 1748, 2623, 3937, 5908, 8863, 13297, 19948, 29923, 44887, 67332, 101001, 151503, 227257, 340887, 511332, 767000 ];
var tt_sb_cv = 0;

var table = $('#awplus_content table:first');

$('tr:eq(0) td:last',table).attr('colspan', 6);
$('tr:eq(1)',table).append('<td>CV</td>');
$('tr:gt(1)',table).each(function() {
  var td = $('td:gt(6)', this);
  var total = 0;
  // If sieged planet is my, dont count fleet.
  if ($(this).attr('bgcolor') == '#602020' && $('td:eq(3)', this).text() != 'N/A') {
    total = '0';
  }
  else if (td.length == 6) {
    total  = db_sb[td[0].innerHTML == 'N/A' ? 0 : parseInt(td[0].innerHTML)];
    tt_sb_cv += total;
    total += parseInt(td[3].innerHTML) * 3;
    total += parseInt(td[4].innerHTML) * 24;
    total += parseInt(td[5].innerHTML) * 60;
  }
  else { // Fleets traveling
    td = $('td:gt(4)', this);
    total  = parseInt(td[0].innerHTML) * 3;
    total += parseInt(td[1].innerHTML) * 24;
    total += parseInt(td[2].innerHTML) * 60;
  }
  
  $(this).append('<td>'+total+'</td>');
});

var ap_cv_totals = '<tr class="ap_cv" bgcolor="#202060"><td align="center" colspan="14"><strong>---- T o t a l s ----</strong></td></tr>';
ap_cv_totals += '<tr class="ap_cv" id="ap_cv_fl" bgcolor="#404040" align="center"><td>Fleets</td><td colspan="6">--</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>--</td></tr>';
ap_cv_totals += '<tr class="ap_cv" id="ap_cv_cv" bgcolor="#404040" align="center"><td>Combat Values</td><td colspan="6">--</td><td>'+tt_sb_cv+'</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>';

$('#awplus_content table:first tr:last').after(ap_cv_totals);

// Totals Fleets
$('#awplus_content table:first tr:gt(1):not(.ap_cv)').each(function() {
  // If sieged planet is my, dont count fleet.
  if ($(this).attr('bgcolor') == '#602020' && $('td:eq(3)', this).text() != 'N/A') {
    return;
  }
  var gt, offset;
  var tt_td = $('td', this).length;
  if (tt_td == 14) { gt = 6; offset = 2; }
  else { gt = 2; offset = 3; }
  
  $('td:gt('+gt+'):not(:last)', this).each(function(i) {
    var td = $('#ap_cv_fl td:eq('+(i+offset)+')');
    var val = parseInt($(this).text());
    if (isNaN(val)) val = 0;
    var total = val + parseInt(td.text());
    td.text(total);
    delete td
  });
});

// Total CV
var ap_CV = [3, 24,60];
var tt = tt_sb_cv;
$('#ap_cv_fl td:gt(4):not(:last)').each(function(i) {
  var td = $('#ap_cv_cv td:eq('+(i+5)+')');
  var total = parseInt($(this).text()) * ap_CV[i];
  tt += total;
  td.text(total);
  delete td;
});
$('#ap_cv_cv td:last').html('<strong>'+tt+'</strong>');


// SHow Systems
$('#awplus_content table:first tr:gt(1):not(.ap_cv)').each(function(i) {
  var td = $('td:eq(0)', this);
  var sid = $.trim(td.text());
  var a = document.createElement('a');
  a.href = "javascript:void(0);";
  a.id = 'ap_sys_'+i+'_'+sid;
  td.wrapInner(a);
  $('#ap_sys_'+i+'_'+sid).click(function() {
    ap_showSystemDialog(sid);
  })
});

// News CV count   ---
ap_showNewsCV('#awplus_content table:eq(1)');

});
